/*
 * Copyright (C) 2003 Yasuhiro Ohara
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
 * Boston, MA 02111-1307, USA.  
 */

#include <zebra.h>

#include "log.h"
#include "memory.h"
#include "linklist.h"
#include "thread.h"
#include "vty.h"
#include "command.h"
#include "if.h"
#include "prefix.h"
#include "table.h"
#include "plist.h"
#include "filter.h"

#include "ospf6_proto.h"
#include "ospf6_lsa.h"
#include "ospf6_lsdb.h"
#include "ospf6_route.h"
#include "ospf6_spf.h"
#include "ospf6_top.h"
#include "ospf6_area.h"
#include "ospf6_interface.h"
#include "ospf6_intra.h"
#include "ospf6_abr.h"
#include "ospf6_af.h"
#include "ospf6_private_data.h"
#include "ospf6d.h"

static struct list ospf6_area_operations_list;
static unsigned int ospf6_area_init_called;

int
ospf6_area_cmp (void *va, void *vb)
{
  struct ospf6_area *oa = (struct ospf6_area *) va;
  struct ospf6_area *ob = (struct ospf6_area *) vb;
  return (ntohl (oa->area_id) < ntohl (ob->area_id) ? -1 : 1);
}

/* schedule routing table recalculation */
static void
ospf6_area_lsdb_hook_add (struct ospf6_lsa *lsa)
{
  switch (ntohs (lsa->header->type))
    {
    case OSPF6_LSTYPE_ROUTER:
    case OSPF6_LSTYPE_NETWORK:
      if (IS_OSPF6_DEBUG_EXAMIN_TYPE (lsa->header->type))
        {
          zlog_debug ("Examin %s", lsa->name);
          zlog_debug ("Schedule SPF Calculation for %s",
		      OSPF6_AREA (lsa->lsdb->data)->name);
        }
      ospf6_spf_schedule (OSPF6_AREA (lsa->lsdb->data));
      break;

    case OSPF6_LSTYPE_INTRA_PREFIX:
      ospf6_intra_prefix_lsa_add (lsa);
      break;

    case OSPF6_LSTYPE_INTER_PREFIX:
    case OSPF6_LSTYPE_INTER_ROUTER:
      ospf6_abr_examin_summary (lsa, (struct ospf6_area *) lsa->lsdb->data);
      break;

    default:
      break;
    }
}

static void
ospf6_area_lsdb_hook_remove (struct ospf6_lsa *lsa)
{
  switch (ntohs (lsa->header->type))
    {
    case OSPF6_LSTYPE_ROUTER:
    case OSPF6_LSTYPE_NETWORK:
      if (IS_OSPF6_DEBUG_EXAMIN_TYPE (lsa->header->type))
        {
          zlog_debug ("LSA disappearing: %s", lsa->name);
          zlog_debug ("Schedule SPF Calculation for %s",
                     OSPF6_AREA (lsa->lsdb->data)->name);
        }
      ospf6_spf_schedule (OSPF6_AREA (lsa->lsdb->data));
      break;

    case OSPF6_LSTYPE_INTRA_PREFIX:
      ospf6_intra_prefix_lsa_remove (lsa);
      break;

    case OSPF6_LSTYPE_INTER_PREFIX:
    case OSPF6_LSTYPE_INTER_ROUTER:
      ospf6_abr_examin_summary (lsa, (struct ospf6_area *) lsa->lsdb->data);
      break;

    default:
      break;
    }
}

static void
ospf6_area_lsdb_hook_replace (struct ospf6_lsa *old, struct ospf6_lsa *new)
{
  u_int16_t type;

  assert (OSPF6_LSA_IS_SAME (old, new));

  type = ntohs (new->header->type);

  if (type == OSPF6_LSTYPE_INTRA_PREFIX)
    {
      ospf6_intra_prefix_lsa_replace (old, new);
    }
  else
    {
      ospf6_area_lsdb_hook_remove (old);
      ospf6_area_lsdb_hook_add (new);
    }
}

static void
ospf6_area_route_hook_add (struct ospf6_route *route)
{
  struct ospf6_route *copy = ospf6_route_copy (route);
  ospf6_route_add (copy, ospf6->route_table);
}

static void
ospf6_area_route_hook_remove (struct ospf6_route *route)
{
  struct ospf6_route *copy;

  copy = ospf6_route_lookup_identical (route, ospf6->route_table);
  if (copy)
    ospf6_route_remove (copy, ospf6->route_table);
}

/* Make new area structure */
struct ospf6_area *
ospf6_area_create (u_int32_t area_id, struct ospf6 *o)
{
  struct ospf6_area *oa;
  struct listnode *node;
  struct ospf6_area_operations *ops;

  oa = XCALLOC (MTYPE_OSPF6_AREA, sizeof (struct ospf6_area));

  ospf6_id2str (area_id, oa->name, sizeof (oa->name));
  oa->area_id = area_id;
  oa->if_list = list_new ();

  oa->lsdb = ospf6_lsdb_create (oa);
  oa->lsdb->hook_add = ospf6_area_lsdb_hook_add;
  oa->lsdb->hook_remove = ospf6_area_lsdb_hook_remove;
  oa->lsdb->hook_replace = ospf6_area_lsdb_hook_replace;
  oa->lsdb_self = ospf6_lsdb_create (oa);

  oa->spf_table = OSPF6_ROUTE_TABLE_CREATE (AREA, SPF_RESULTS);
  oa->spf_table->scope = oa;
  oa->route_table = OSPF6_ROUTE_TABLE_CREATE (AREA, ROUTES);
  oa->route_table->scope = oa;
  oa->route_table->hook_add = ospf6_area_route_hook_add;
  oa->route_table->hook_remove = ospf6_area_route_hook_remove;

  oa->range_table = OSPF6_ROUTE_TABLE_CREATE (AREA, PREFIX_RANGES);
  oa->range_table->scope = oa;
  oa->summary_prefix = OSPF6_ROUTE_TABLE_CREATE (AREA, SUMMARY_PREFIXES);
  oa->summary_prefix->scope = oa;
  oa->summary_router = OSPF6_ROUTE_TABLE_CREATE (AREA, SUMMARY_ROUTERS);
  oa->summary_router->scope = oa;
  oa->inter_area_id = 0;

  /* set default options */
  OSPF6_OPT_SET (oa->options, OSPF6_OPT_V6, 2);
  OSPF6_OPT_SET (oa->options, OSPF6_OPT_E, 2);
  OSPF6_OPT_SET (oa->options, OSPF6_OPT_R, 2);

  /*
   * RFC 5838 2.2:
   *
   * When an OSPFv3 router is supporting AFs as described in this
   * specification, it MUST set the AF-bit in the OSPFv3 Options field
   * of Hello packets, Database Description packets, and LSAs.
   */
  OSPF6_OPT_SET (oa->options, OSPF6_OPT_AF, 1);

  oa->spf_delay_msec = OSPF6_DEFAULT_SPF_DELAY_MSEC;
  oa->spf_holdtime_msec = OSPF6_DEFAULT_SPF_HOLDTIME_MSEC;

  oa->ospf6 = o;
  listnode_add_sort (o->area_list, oa);

  oa->private_data_list = ospf6_private_data_list ();

  for (ALL_LIST_ELEMENTS_RO (&ospf6_area_operations_list, node, ops))
    {
      int err;

      if (!ops->create)
	continue;

      err = ops->create (oa);
      if (err)
	{
	  zlog_err ("%s: per area create function %p failed for area %s",
		    __func__, ops->create, oa->name);
	  ospf6_area_delete (oa);
	  return NULL;
	}
    }

  return oa;
}

void
ospf6_area_delete (struct ospf6_area *oa)
{
  struct listnode *n, *nnode;
  struct ospf6_interface *oi;

  ospf6_route_table_delete (oa->range_table);
  ospf6_route_table_delete (oa->summary_prefix);
  ospf6_route_table_delete (oa->summary_router);

  /* ospf6 interface list */
  for (ALL_LIST_ELEMENTS (oa->if_list, n, nnode, oi))
    {
      ospf6_interface_delete (oi);
    }
  list_delete (oa->if_list);

  ospf6_lsdb_delete (oa->lsdb);
  ospf6_lsdb_delete (oa->lsdb_self);

  ospf6_spf_table_finish (oa->spf_table);
  ospf6_route_table_delete (oa->spf_table);
  ospf6_route_table_delete (oa->route_table);

  THREAD_OFF (oa->thread_spf_calculation);

  for (n = listtail (&ospf6_area_operations_list); n != NULL; n = n->prev)
    {
      struct ospf6_area_operations *ops;

      ops = listgetdata (n);
      if (ops && ops->delete)
	ops->delete (oa);
    }

  listnode_delete (oa->ospf6->area_list, oa);
  oa->ospf6 = NULL;

  if (!list_isempty (oa->private_data_list))
    zlog_err ("%s: possible memory leak: deleting oa->private_data_list "
	      "with %i elements", __func__, listcount (oa->private_data_list));
  list_delete (oa->private_data_list);

  /* free area */
  XFREE (MTYPE_OSPF6_AREA, oa);
}

/**
 * Register area operations
 *
 * Area operations are a set of callbacks that apply to all ospf
 * areas.
 *
 * @param ops A pointer to the area operations structure to
 *            register.  The pointer must remain valid as long as it
 *            remains registered.
 *
 * @return Zero on success.  Nonzero if an error occurred.
 */
int
ospf6_area_register_operations (struct ospf6_area_operations *ops)
{
  struct listnode *node;
  struct ospf6_area_operations *tmpops;

  for (ALL_LIST_ELEMENTS_RO (&ospf6_area_operations_list, node, tmpops))
    {
      if (tmpops == ops)
	{
	  zlog_err ("%s: per area operations already registered: %p",
		    __func__, ops);
	  return -1;
	}
    }

  listnode_add (&ospf6_area_operations_list, ops);

  if (ospf6_area_init_called && ops->init)
    ops->init ();

  if (ops->create && ospf6)
    {
      struct ospf6_area *oa;

      for (ALL_LIST_ELEMENTS_RO (ospf6->area_list, node, oa))
	{
	  int err;

	  err = ops->create (oa);
	  if (err)
	    zlog_warn ("%s: per area create function %p failed for area %s",
		       __func__, ops->create, oa->name);
	}
    }

  return 0;
}

/**
 * Add private data to an ospf area
 *
 * Private data can be associated with an ospf area by calling
 * this function.  A unique data identifier, meant to be stable across
 * all areas, is used to refer to the data.  A new identifier is
 * assigned to the location pointed to by @c id when its current value
 * is zero.  Otherwise the current value stored in @c id is used,
 * which must not already be in use for the given area.
 *
 * @param oa The ospf area.
 *
 * @param id A pointer to the data id.
 *
 * @param data A pointer to the data.
 *
 * @return Zero on success.  Nonzero if an error occurred.
 */
int
ospf6_area_add_data (struct ospf6_area *oa, unsigned int *id, void *data)
{
  return ospf6_add_private_data (oa->private_data_list, id, data);
}

/**
 * Get private data associated with an ospf area
 *
 * The given data identifier should be from an earlier
 * ospf6_area_add_data() call and must be nonzero.
 *
 * @param oa The ospf area.
 *
 * @param id The data identifier.
 *
 * @return A pointer to the data reference by @c id or NULL if no data
 *         is found for the given identifier.
 */
void *
ospf6_area_get_data (struct ospf6_area *oa, unsigned int id)
{
  return ospf6_get_private_data (oa->private_data_list, id);
}

/**
 * Get and remove private data associated with an ospf area
 *
 * The given data identifier should be from an earlier
 * ospf6_area_add_data() call and must be nonzero.
 *
 * @param oa The ospf area.
 *
 * @param id The data identifier.
 *
 * @return A pointer to the data reference by @c id or NULL if no data
 *         is found for the given identifier.
 */
void *
ospf6_area_del_data (struct ospf6_area *oa, unsigned int id)
{
  return ospf6_del_private_data (oa->private_data_list, id);
}

struct ospf6_area *
ospf6_area_lookup (u_int32_t area_id, struct ospf6 *ospf6)
{
  struct ospf6_area *oa;
  struct listnode *n;

  for (ALL_LIST_ELEMENTS_RO (ospf6->area_list, n, oa))
    if (oa->area_id == area_id)
      return oa;

  return (struct ospf6_area *) NULL;
}

struct ospf6_area *
ospf6_area_get (u_int32_t area_id, struct ospf6 *o)
{
  struct ospf6_area *oa;
  oa = ospf6_area_lookup (area_id, o);
  if (oa == NULL)
    oa = ospf6_area_create (area_id, o);
  return oa;
}

void
ospf6_area_enable (struct ospf6_area *oa)
{
  struct listnode *node, *nnode;
  struct ospf6_interface *oi;

  SET_FLAG (oa->flag, OSPF6_AREA_ENABLE);

  for (ALL_LIST_ELEMENTS (oa->if_list, node, nnode, oi))
    ospf6_interface_enable (oi);

  if (ospf6_is_router_abr (oa->ospf6))
    ospf6_abr_enable_area (oa);
}

void
ospf6_area_disable (struct ospf6_area *oa)
{
  struct listnode *node, *nnode;
  struct ospf6_interface *oi;

  if (! CHECK_FLAG (oa->flag, OSPF6_AREA_ENABLE))
    return;

  UNSET_FLAG (oa->flag, OSPF6_AREA_ENABLE);

  THREAD_OFF (oa->thread_spf_calculation);

  for (ALL_LIST_ELEMENTS (oa->if_list, node, nnode, oi))
    ospf6_interface_disable (oi);

  ospf6_route_remove_all (oa->range_table);
  ospf6_route_remove_all (oa->summary_prefix);
  ospf6_route_remove_all (oa->summary_router);

  ospf6_lsdb_remove_all (oa->lsdb);
  ospf6_lsdb_remove_all (oa->lsdb_self);

  ospf6_spf_table_finish (oa->spf_table);
  ospf6_route_remove_all (oa->spf_table);
  ospf6_route_remove_all (oa->route_table);
}


void
ospf6_area_show (struct vty *vty, struct ospf6_area *oa)
{
  struct listnode *i;
  struct ospf6_interface *oi;

  vty_out (vty, " Area %s%s", oa->name, VNL);
  vty_out (vty, "     Number of Area scoped LSAs is %u%s",
           oa->lsdb->count, VNL);

  vty_out (vty, "     Interface attached to this area:");
  for (ALL_LIST_ELEMENTS_RO (oa->if_list, i, oi))
    vty_out (vty, " %s", oi->interface->name);
  
  vty_out (vty, "%s", VNL);
}

#define OSPF6_CMD_AREA_LOOKUP(str, oa)                     \
{                                                          \
  u_int32_t area_id = 0;                                   \
  if (ospf6_str2id (str, &area_id))			   \
    {                                                      \
      vty_out (vty, "Malformed Area-ID: %s%s", str, VNL);  \
      return CMD_WARNING;                                  \
    }                                                      \
  oa = ospf6_area_lookup (area_id, ospf6);                 \
  if (oa == NULL)                                          \
    {                                                      \
      vty_out (vty, "No such Area: %s%s", str, VNL);       \
      return CMD_WARNING;                                  \
    }                                                      \
}

#define OSPF6_CMD_AREA_GET(str, oa)                        \
{                                                          \
  u_int32_t area_id = 0;                                   \
  if (ospf6_str2id (str, &area_id))			   \
    {                                                      \
      vty_out (vty, "Malformed Area-ID: %s%s", str, VNL);  \
      return CMD_WARNING;                                  \
    }                                                      \
  oa = ospf6_area_get (area_id, ospf6);                    \
}

DEFUN (area_range,
       area_range_cmd,
       "area (A.B.C.D|<0-4294967295>) range (X:X::X:X/M|A.B.C.D/M)",
       "OSPF area parameters\n"
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       "Configured address range\n"
       "Specify IPv6 prefix\n"
       "Specify IPv4 prefix\n"
       )
{
  int ret;
  struct ospf6_area *oa;
  struct prefix prefix;
  struct ospf6_route *range;

  OSPF6_CMD_AREA_GET (argv[0], oa);
  argc--;
  argv++;

  ret = ospf6_str2prefix (oa->ospf6, argv[0], &prefix);
  if (ret == 0)
    {
      vty_out (vty, "Malformed argument: %s%s", argv[0], VNL);
      return CMD_SUCCESS;
    }
  argc--;
  argv++;

  range = ospf6_route_lookup (&prefix, oa->range_table);
  if (range == NULL)
    {
      range = ospf6_route_create ();
      range->type = OSPF6_DEST_TYPE_RANGE;
      range->prefix = prefix;
    }

  if (argc)
    {
      if (! strcmp (argv[0], "not-advertise"))
        SET_FLAG (range->flag, OSPF6_ROUTE_DO_NOT_ADVERTISE);
      else if (! strcmp (argv[0], "advertise"))
        UNSET_FLAG (range->flag, OSPF6_ROUTE_DO_NOT_ADVERTISE);
    }

  if (range->rnode)
    {
      vty_out (vty, "Range already defined: %s%s", argv[-1], VNL);
      return CMD_WARNING;
    }

  ospf6_route_add (range, oa->range_table);
  return CMD_SUCCESS;
}

ALIAS (area_range,
       area_range_advertise_cmd,
       "area (A.B.C.D|<0-4294967295>) range (X:X::X:X/M|A.B.C.D/M) (advertise|not-advertise)",
       "OSPF area parameters\n"
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       "Configured address range\n"
       "Specify IPv6 prefix\n"
       "Specify IPv4 prefix\n"
       )

DEFUN (no_area_range,
       no_area_range_cmd,
       "no area (A.B.C.D|<0-4294967295>) range (X:X::X:X/M|A.B.C.D/M)",
       NO_STR
       "OSPF area parameters\n"
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       "Configured address range\n"
       "Specify IPv6 prefix\n"
       "Specify IPv4 prefix\n"
       )
{
  int ret;
  struct ospf6_area *oa;
  struct prefix prefix;
  struct ospf6_route *range;

  OSPF6_CMD_AREA_GET (argv[0], oa);
  argc--;
  argv++;

  ret = ospf6_str2prefix (oa->ospf6, argv[0], &prefix);
  if (ret == 0)
    {
      vty_out (vty, "Malformed argument: %s%s", argv[0], VNL);
      return CMD_SUCCESS;
    }

  range = ospf6_route_lookup (&prefix, oa->range_table);
  if (range == NULL)
    {
      vty_out (vty, "Range %s does not exists.%s", argv[0], VNL);
      return CMD_SUCCESS;
    }

  ospf6_route_remove (range, oa->range_table);
  return CMD_SUCCESS;
}

void
ospf6_area_config_write (struct vty *vty)
{
  struct listnode *node;
  struct ospf6_area *oa;
  struct ospf6_route *range;
  char buf[128];

  for (ALL_LIST_ELEMENTS_RO (ospf6->area_list, node, oa))
    {
      struct listnode *node;
      struct ospf6_area_operations *ops;

      for (range = ospf6_route_head (oa->range_table); range;
           range = ospf6_route_next (range))
        {
          ospf6_prefix2str (oa->ospf6, &range->prefix, buf, sizeof (buf));
          vty_out (vty, " area %s range %s%s", oa->name, buf, VNL);
        }
      if (PREFIX_NAME_IN (oa))
        vty_out (vty, " area %s filter-list prefix %s in%s",
                 oa->name, PREFIX_NAME_IN (oa), VNL);
      if (PREFIX_NAME_OUT (oa))
        vty_out (vty, " area %s filter-list prefix %s out%s",
                 oa->name, PREFIX_NAME_OUT (oa), VNL);
      if (IMPORT_NAME (oa))
        vty_out (vty, " area %s import-list %s%s",
                 oa->name, IMPORT_NAME (oa), VNL);
      if (EXPORT_NAME (oa))
        vty_out (vty, " area %s export-list %s%s",
                 oa->name, EXPORT_NAME (oa), VNL);
      if (oa->spf_delay_msec != OSPF6_DEFAULT_SPF_DELAY_MSEC)
	vty_out (vty, " area %s spf-delay-msec %u%s",
		 oa->name, oa->spf_delay_msec, VNL);
      if (oa->spf_holdtime_msec != OSPF6_DEFAULT_SPF_HOLDTIME_MSEC)
	vty_out (vty, " area %s spf-holdtime-msec %u%s",
		 oa->name, oa->spf_holdtime_msec, VNL);

      for (ALL_LIST_ELEMENTS_RO (&ospf6_area_operations_list, node, ops))
	if (ops && ops->config_write)
	  ops->config_write (oa, vty);
    }
}

DEFUN (area_filter_list,
       area_filter_list_cmd,
       "area (A.B.C.D|<0-4294967295>) filter-list prefix WORD (in|out)",
       "OSPFv6 area parameters\n"
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       "Filter networks between OSPFv6 areas\n"
       "Filter prefixes between OSPFv6 areas\n"
       "Name of an IPv6 prefix-list\n"
       "Filter networks sent to this area\n"
       "Filter networks sent from this area\n")
{
  struct ospf6_area *area;
  struct prefix_list *plist;

  OSPF6_CMD_AREA_GET (argv[0], area);
  argc--;
  argv++;

  plist = prefix_list_lookup (AFI_IP6, argv[0]);
  if (strncmp (argv[1], "in", 2) == 0)
    {
      PREFIX_LIST_IN (area) = plist;
      if (PREFIX_NAME_IN (area))
	free (PREFIX_NAME_IN (area));

      PREFIX_NAME_IN (area) = strdup (argv[0]);
      ospf6_abr_reimport (area);
    }
  else
    {
      PREFIX_LIST_OUT (area) = plist;
      if (PREFIX_NAME_OUT (area))
	free (PREFIX_NAME_OUT (area));

      PREFIX_NAME_OUT (area) = strdup (argv[0]);
      ospf6_abr_enable_area (area);
    }

  return CMD_SUCCESS;
}
     
DEFUN (no_area_filter_list,
       no_area_filter_list_cmd,
       "no area (A.B.C.D|<0-4294967295>) filter-list prefix WORD (in|out)",
       NO_STR
       "OSPFv6 area parameters\n"
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       "Filter networks between OSPFv6 areas\n"
       "Filter prefixes between OSPFv6 areas\n"
       "Name of an IPv6 prefix-list\n"
       "Filter networks sent to this area\n"
       "Filter networks sent from this area\n")
{
  struct ospf6_area *area;
  struct prefix_list *plist;

  OSPF6_CMD_AREA_GET (argv[0], area);
  argc--;
  argv++;

  plist = prefix_list_lookup (AFI_IP6, argv[0]);
  if (strncmp (argv[1], "in", 2) == 0)
    {
      if (PREFIX_NAME_IN (area))
	if (strcmp (PREFIX_NAME_IN (area), argv[0]) != 0)
	  return CMD_SUCCESS;

      PREFIX_LIST_IN (area) = NULL;
      if (PREFIX_NAME_IN (area))
	free (PREFIX_NAME_IN (area));

      PREFIX_NAME_IN (area) = NULL;
      ospf6_abr_reimport (area);
    }
  else
    {
      if (PREFIX_NAME_OUT (area))
	if (strcmp (PREFIX_NAME_OUT (area), argv[0]) != 0)
	  return CMD_SUCCESS;

      PREFIX_LIST_OUT (area) = NULL;
      if (PREFIX_NAME_OUT (area))
	free (PREFIX_NAME_OUT (area));

      PREFIX_NAME_OUT (area) = NULL;
      ospf6_abr_enable_area (area);
    }

  return CMD_SUCCESS;
}

DEFUN (area_import_list,
       area_import_list_cmd,
       "area (A.B.C.D|<0-4294967295>) import-list NAME",
       "OSPFv6 area parameters\n"
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       "Set the filter for networks from other areas announced to the specified one\n"
       "Name of the acess-list\n")
{
  struct ospf6_area *area;
  struct access_list *list;

  OSPF6_CMD_AREA_GET(argv[0], area);

  list = access_list_lookup (AFI_IP6, argv[1]);

  IMPORT_LIST (area) = list;

  if (IMPORT_NAME (area))
    free (IMPORT_NAME (area));

  IMPORT_NAME (area) = strdup (argv[1]);
  ospf6_abr_reimport (area);

  return CMD_SUCCESS; 
}

DEFUN (no_area_import_list,
       no_area_import_list_cmd,
       "no area (A.B.C.D|<0-4294967295>) import-list NAME",
       "OSPFv6 area parameters\n"
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       "Unset the filter for networks announced to other areas\n"
       "NAme of the access-list\n")
{
  struct ospf6_area *area;

  OSPF6_CMD_AREA_GET(argv[0], area);

  IMPORT_LIST (area) = 0;

  if (IMPORT_NAME (area))
    free (IMPORT_NAME (area));

  IMPORT_NAME (area) = NULL;
  ospf6_abr_reimport (area);

  return CMD_SUCCESS;
}

DEFUN (area_export_list,
       area_export_list_cmd,
       "area (A.B.C.D|<0-4294967295>) export-list NAME",
       "OSPFv6 area parameters\n"
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       "Set the filter for networks announced to other areas\n"
       "Name of the acess-list\n")
{
  struct ospf6_area *area;
  struct access_list *list;

  OSPF6_CMD_AREA_GET(argv[0], area);

  list = access_list_lookup (AFI_IP6, argv[1]);

  EXPORT_LIST (area) = list;

  if (EXPORT_NAME (area))
    free (EXPORT_NAME (area));

  EXPORT_NAME (area) = strdup (argv[1]);
  ospf6_abr_enable_area (area);

  return CMD_SUCCESS; 
}

DEFUN (no_area_export_list,
       no_area_export_list_cmd,
       "no area (A.B.C.D|<0-4294967295>) export-list NAME",
       "OSPFv6 area parameters\n"
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       "Unset the filter for networks announced to other areas\n"
       "Name of the access-list\n")
{
  struct ospf6_area *area;

  OSPF6_CMD_AREA_GET(argv[0], area);

  EXPORT_LIST (area) = 0;

  if (EXPORT_NAME (area))
    free (EXPORT_NAME (area));

  EXPORT_NAME (area) = NULL;
  ospf6_abr_enable_area (area);

  return CMD_SUCCESS;
}

DEFUN (area_spf_delay_msec,
       area_spf_delay_msec_cmd,
       "area (A.B.C.D|<0-4294967295>) spf-delay-msec <0-10000>",
       "OSPFv6 area parameters\n"
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       "Time to delay SPF calculations\n"
       "Milliseconds\n")
{
  struct ospf6_area *oa;

  OSPF6_CMD_AREA_GET(argv[0], oa);

  oa->spf_delay_msec = strtol (argv[1], NULL, 10);

  return CMD_SUCCESS;
}

DEFUN (area_spf_holdtime_msec,
       area_spf_holdtime_msec_cmd,
       "area (A.B.C.D|<0-4294967295>) spf-holdtime-msec <0-10000>",
       "OSPFv6 area parameters\n"
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       "Minimum time between SPF calculations\n"
       "Milliseconds\n")
{
  struct ospf6_area *oa;

  OSPF6_CMD_AREA_GET(argv[0], oa);

  oa->spf_holdtime_msec = strtol (argv[1], NULL, 10);

  return CMD_SUCCESS;
}

DEFUN (show_ipv6_ospf6_spf_tree,
       show_ipv6_ospf6_spf_tree_cmd,
       "show ipv6 ospf6 spf tree",
       SHOW_STR
       IP6_STR
       OSPF6_STR
       "Shortest Path First caculation\n"
       "Show SPF tree\n")
{
  struct listnode *node;
  struct ospf6_area *oa;
  struct ospf6_vertex *root;
  struct ospf6_route *route;
  struct prefix prefix;

  ospf6_linkstate_prefix (ospf6->router_id, htonl (0), &prefix);

  for (ALL_LIST_ELEMENTS_RO (ospf6->area_list, node, oa))
    {
      route = ospf6_route_lookup (&prefix, oa->spf_table);
      if (route == NULL)
        {
          vty_out (vty, "LS entry for root not found in area %s%s",
                   oa->name, VNL);
          continue;
        }
      root = (struct ospf6_vertex *) route->route_option;
      ospf6_spf_display_subtree (vty, "", 0, root);
    }

  return CMD_SUCCESS;
}

DEFUN (show_ipv6_ospf6_area_spf_tree,
       show_ipv6_ospf6_area_spf_tree_cmd,
       "show ipv6 ospf6 area (A.B.C.D|<0-4294967295>) spf tree",
       SHOW_STR
       IP6_STR
       OSPF6_STR
       OSPF6_AREA_STR
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       "Shortest Path First caculation\n"
       "Show SPF tree\n")
{
  struct ospf6_area *oa;
  struct ospf6_vertex *root;
  struct ospf6_route *route;
  struct prefix prefix;

  ospf6_linkstate_prefix (ospf6->router_id, htonl (0), &prefix);

  OSPF6_CMD_AREA_LOOKUP (argv[0], oa);

  route = ospf6_route_lookup (&prefix, oa->spf_table);
  if (route == NULL)
    {
      vty_out (vty, "LS entry for root not found in area %s%s",
               oa->name, VNL);
      return CMD_SUCCESS;
    }
  root = (struct ospf6_vertex *) route->route_option;
  ospf6_spf_display_subtree (vty, "", 0, root);

  return CMD_SUCCESS;
}

DEFUN (show_ipv6_ospf6_simulate_spf_tree_root,
       show_ipv6_ospf6_simulate_spf_tree_root_cmd,
       "show ipv6 ospf6 simulate spf-tree A.B.C.D area (A.B.C.D|<0-4294967295>)",
       SHOW_STR
       IP6_STR
       OSPF6_STR
       "Shortest Path First caculation\n"
       "Show SPF tree\n"
       "Specify root's router-id to calculate another router's SPF tree\n"
       OSPF6_AREA_STR
       OSPF6_AREAID_DOT_STR
       OSPF6_AREAID_VAL_STR
       )
{
  struct ospf6_area *oa;
  struct ospf6_vertex *root;
  struct ospf6_route *route;
  struct prefix prefix;
  u_int32_t router_id;
  struct ospf6_route_table *spf_table;
  unsigned char tmp_debug_ospf6_spf = 0;

  ospf6_str2id (argv[0], &router_id);
  ospf6_linkstate_prefix (router_id, htonl (0), &prefix);

  OSPF6_CMD_AREA_LOOKUP (argv[1], oa);

  tmp_debug_ospf6_spf = conf_debug_ospf6_spf;
  conf_debug_ospf6_spf = 0;

  spf_table = OSPF6_ROUTE_TABLE_CREATE (NONE, SPF_RESULTS);
  ospf6_spf_calculation (router_id, spf_table, oa);

  conf_debug_ospf6_spf = tmp_debug_ospf6_spf;

  route = ospf6_route_lookup (&prefix, spf_table);
  if (route == NULL)
    {
      vty_out (vty, "LS entry for root not found in area %s%s",
	       oa->name, VNL);

      ospf6_spf_table_finish (spf_table);
      ospf6_route_table_delete (spf_table);
      return CMD_SUCCESS;
    }
  root = (struct ospf6_vertex *) route->route_option;
  ospf6_spf_display_subtree (vty, "", 0, root);

  ospf6_spf_table_finish (spf_table);
  ospf6_route_table_delete (spf_table);

  return CMD_SUCCESS;
}

void
ospf6_area_init (void)
{
  struct listnode *node;
  struct ospf6_area_operations *ops;

  install_element (VIEW_NODE, &show_ipv6_ospf6_spf_tree_cmd);
  install_element (VIEW_NODE, &show_ipv6_ospf6_area_spf_tree_cmd);
  install_element (VIEW_NODE, &show_ipv6_ospf6_simulate_spf_tree_root_cmd);

  install_element (ENABLE_NODE, &show_ipv6_ospf6_spf_tree_cmd);
  install_element (ENABLE_NODE, &show_ipv6_ospf6_area_spf_tree_cmd);
  install_element (ENABLE_NODE, &show_ipv6_ospf6_simulate_spf_tree_root_cmd);

  install_element (OSPF6_NODE, &area_range_cmd);
  install_element (OSPF6_NODE, &area_range_advertise_cmd);
  install_element (OSPF6_NODE, &no_area_range_cmd);

  install_element (OSPF6_NODE, &area_import_list_cmd);
  install_element (OSPF6_NODE, &no_area_import_list_cmd);
  install_element (OSPF6_NODE, &area_export_list_cmd);
  install_element (OSPF6_NODE, &no_area_export_list_cmd);

  install_element (OSPF6_NODE, &area_filter_list_cmd);
  install_element (OSPF6_NODE, &no_area_filter_list_cmd);

  install_element (OSPF6_NODE, &area_spf_delay_msec_cmd);
  install_element (OSPF6_NODE, &area_spf_holdtime_msec_cmd);

  for (ALL_LIST_ELEMENTS_RO (&ospf6_area_operations_list, node, ops))
    if (ops && ops->init)
      ops->init ();

  ospf6_area_init_called++;
}

void
ospf6_area_terminate (void)
{
  list_delete_all_node (&ospf6_area_operations_list);
}
